//
//  findView.m
//  f_king
//
//  Created by cherry on 10-9-23.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "findView.h"


@implementation findView

@synthesize findTarget;
@synthesize origin;
@synthesize selectRec;
@synthesize lable;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        self.findTarget = [UIImage imageNamed:@"target.jpg"];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch * touch = [touches anyObject];
	self.origin = [touch locationInView:self];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch * touch = [touches anyObject];
	CGPoint pt = [touch locationInView:self];
	CGRect rec = [self findFrameWithPt1:pt Pt2:origin];
	[self drawRect:rec];
	//selectRec = [[UIView alloc] initWithFrame:rec];
	//[selectRec setBackgroundColor:[UIColor lightGrayColor]];
	//[selectRec setAlpha:0.3];
	//[[self.subviews lastObject] removeFromSuperview]; 
	//[self addSubview:selectRec];
	//[selectRec release];
}

-(CGRect) findFrameWithPt1:(CGPoint)pt1 Pt2:(CGPoint) pt2{
	CGFloat tmp;
	if (pt1.x > pt2.x) {
		tmp = pt1.x;
		pt1.x = pt2.x;
		pt2.x = tmp;
	}
	if (pt1.y > pt2.y) {
		tmp = pt1.y;
		pt1.y = pt2.y;
		pt2.y = tmp;
	}
	return CGRectMake(pt1.x, pt1.y, pt2.x-pt1.x, pt2.y-pt2.x);
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
	UITouch * touch = [touches anyObject];
	CGPoint pt = [touch locationInView:self];
	NSString *location = [NSString stringWithFormat:@"( %f, %f )\n\t-->\n( %f, %f )",pt.x,pt.y,origin.x,origin.y];
	//UIAlertView *showPt = [[UIAlertView alloc] initWithTitle:@"Rec" message:location delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
	//[showPt show];
	//[showPt release];
	lable.text = location;
}

- (void)drawRect:(CGRect)frame {
	//CGPoint leftTop = CGPointMake(200, 200);
//	[self.findTarget drawAtPoint:leftTop];

	CGRect fresh = CGRectMake(0,0,768,1024);
	[self setNeedsDisplayInRect: fresh];
	//[image drawAtPoint:currentPoint];
	[UIImageView commitAnimations];
}

- (void)dealloc {
	[findTarget release];
    [super dealloc];
}

- (void)draw {
	
}

@end
